<template>
  <div class="notification-page">
    <div class="container">
      <!-- 左侧分类列表 -->
      <div class="sidebar">
        <div class="sidebar-title">消息通知</div>
        <el-menu 
          default-active="all" 
          class="notification-menu"
          @select="handleCategoryChange"
        >
          <el-menu-item index="all">
            全部消息
            <el-badge :value="12" class="badge-all" />
          </el-menu-item>
          <el-menu-item index="system">
            系统通知
            <el-badge :value="5" class="badge-system" />
          </el-menu-item>
          <el-menu-item index="finance">
            财务通知
            <el-badge :value="3" class="badge-finance" />
          </el-menu-item>
          <el-menu-item index="operation">
            运营通知
            <el-badge :value="4" class="badge-operation" />
          </el-menu-item>
          <el-menu-item index="read">
            已读消息
          </el-menu-item>
          <el-menu-item index="deleted">
            回收站
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 右侧收件箱区域 -->
      <div class="content">
        <div class="content-header">
          <h2>收件箱</h2>
          <el-button 
            icon="Refresh" 
            size="small" 
            @click="handleRefresh"
            class="refresh-btn"
          ></el-button>
        </div>

        <!-- 消息表格 -->
        <el-table 
          :data="messageList" 
          border 
          stripe
          style="width: 100%; margin-top: 16px"
          @selection-change="handleSelectionChange"
        >
          <el-table-column 
            type="selection" 
            width="50"
          ></el-table-column>

          <el-table-column label="通知标题" prop="title" width="200"></el-table-column>

          <el-table-column label="标签" width="120">
            <template #default="scope">
              <el-tag 
                :type="scope.row.tagType === 'afternoon' ? 'warning' : 'danger'"
                size="small"
              >
                {{ scope.row.tagText }}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column label="通知内容" prop="content"></el-table-column>

          <el-table-column label="附件" width="60">
            <template #default="scope">
              <el-icon v-if="scope.row.hasAttachment" class="attachment-icon">
                <Document />
              </el-icon>
            </template>
          </el-table-column>

          <el-table-column label="时间" prop="time" width="160"></el-table-column>
        </el-table>

        <!-- 分页控件 -->
        <div class="pagination-container">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pagination.currentPage"
            :page-sizes="[10, 20, 50]"
            :page-size="pagination.pageSize"
            layout="prev, pager, next"
            :total="pagination.total"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Refresh, Document } from '@element-plus/icons-vue'

// 分类切换
const handleCategoryChange = (index) => {
  console.log('切换到分类：', index)
  // 切换分类时重置分页并加载对应数据
  pagination.currentPage = 1
  fetchMessageList(index)
}

// 刷新消息列表
const handleRefresh = () => {
  ElMessage.success('消息已刷新')
  fetchMessageList(currentCategory.value)
}

// 选择消息
const selectedMessages = ref([])
const handleSelectionChange = (val) => {
  selectedMessages.value = val
}

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 当前选中分类
const currentCategory = ref('all')

// 消息列表数据
const messageList = ref([])

// 模拟消息数据
const mockMessages = {
  all: [
    {
      id: '1',
      title: '系统升级通知',
      tagType: 'system',
      tagText: '系统通知',
      content: '您好，系统将于今晚23:00进行升级维护，预计持续2小时，期间可能无法正常使用，敬请谅解',
      hasAttachment: false,
      time: '2023-10-15 09:30:22'
    },
    {
      id: '2',
      title: '您的款项已到账',
      tagType: 'finance',
      tagText: '打款通知',
      content: '您的订单#20231015001款项已到账，金额：¥12,500.00，请查收',
      hasAttachment: true,
      time: '2023-10-15 14:25:10'
    },
    {
      id: '3',
      title: '下午会议提醒',
      tagType: 'afternoon',
      tagText: '下午通知',
      content: '今天下午15:00将召开月度总结会议，请准时参加，会议地点：3楼会议室',
      hasAttachment: false,
      time: '2023-10-15 13:10:05'
    },
    {
      id: '4',
      title: '新功能上线通知',
      tagType: 'operation',
      tagText: '运营通知',
      content: '平台新功能"数据分析"已上线，您可以在控制台查看详细数据报表',
      hasAttachment: false,
      time: '2023-10-14 16:45:30'
    },
    {
      id: '5',
      title: '发票已开具',
      tagType: 'finance',
      tagText: '打款通知',
      content: '您申请的发票已开具，发票号码：00123456，已邮寄至您的地址',
      hasAttachment: true,
      time: '2023-10-14 10:20:18'
    }
  ],
  system: [/* 系统通知数据子集 */],
  finance: [/* 财务通知数据子集 */],
  operation: [/* 运营通知数据子集 */]
}

// 初始化时加载全部消息
onMounted(() => {
  fetchMessageList('all')
})

// 获取消息列表
const fetchMessageList = (category) => {
  // 模拟接口请求
  setTimeout(() => {
    // 实际项目中根据分类请求不同数据
    messageList.value = mockMessages[category] || mockMessages.all
    pagination.total = messageList.value.length
  }, 300)
}

// 分页大小改变
const handleSizeChange = (val) => {
  pagination.pageSize = val
}

// 当前页改变
const handleCurrentChange = (val) => {
  pagination.currentPage = val
}
</script>

<style scoped>
.notification-page {
  background-color: #f5f7fa;
  min-height: 100vh;
  padding: 20px;
}

.container {
  display: flex;
  gap: 20px;
  max-width: 1400px;
  margin: 0 auto;
}

/* 左侧分类栏 */
.sidebar {
  width: 220px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.sidebar-title {
  padding: 16px 20px;
  font-size: 16px;
  font-weight: 700;
  border-bottom: 1px solid #f0f0f0;
}

.notification-menu {
  border-right: none;
}

::v-deep .el-menu-item {
  padding-left: 30px !important;
  height: 50px;
  line-height: 50px;
}

/* 自定义徽章颜色 */
::v-deep .badge-all .el-badge__content {
  background-color: #ff4d4f;
}

::v-deep .badge-system .el-badge__content {
  background-color: #fa8c16;
}

::v-deep .badge-finance .el-badge__content {
  background-color: #1890ff;
}

::v-deep .badge-operation .el-badge__content {
  background-color: #52c41a;
}

/* 右侧内容区 */
.content {
  flex: 1;
  background-color: #fff;
  border-radius: 4px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.content-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.content-header h2 {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
}

.refresh-btn {
  color: #1890ff;
  background-color: #e6f7ff;
}

/* 表格样式 */
::v-deep .el-table .el-table__cell {
  padding: 12px 0;
}

.attachment-icon {
  color: #1890ff;
  font-size: 18px;
}

/* 分页样式 */
.pagination-container {
  margin-top: 20px;
  text-align: right;
}

::v-deep .el-pagination {
  display: inline-flex;
}
</style>